package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.thread.ThreadPoolUtil;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IRePurchaseService;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 患者复购
 *
 * @author fanjd
 * @since 1.0.0
 */
@Controller
@RequestMapping("ad/rePurchase")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.MEDICINE_DRUG})
public class AdRepurchaseController {
    @Autowired
    private IRePurchaseService rePurchaseService;

    /**
     * 根据患者id更新患者的发送时间
     * RepurchaseController.updateSendCycle()
     *
     * @param patientId
     *
     * @return
     *
     * @throws BusinessException
     * @Author fanjd
     * @Date 2017年6月12日
     * @since 1.0.0
     */
    @RequestMapping("/updateSendCycle")
    @ResponseBody
    @ApiIgnore
    //TODO 确定干嘛用的
    public ResponseVO<Void> updateSendCycle(final Long patientId) throws BusinessException, Exception {
        if (null == patientId) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        ThreadPoolUtil.execute(() -> this.rePurchaseService.updateSendCycle(patientId));
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }


    /**
     * 每个月1号凌晨3点执行删除过期的任务
     * RepurchaseController.removeExpireTask()
     *
     * @return
     *
     * @throws Exception
     * @Author fanjd
     * @Date 2017年6月19日
     * @since 1.0.0
     */
    @RequestMapping("remove")
    @ApiIgnore
    @ResponseBody
    //TODO 确定干嘛用的
    public ResponseVO<Void> removeExpireTask() {
        this.rePurchaseService.removeExpireTask();
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
